/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managerBeans;

import entity.Feedback;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import org.primefaces.context.RequestContext;
import sessionBeans.FeedbackFacadeLocal;
import util.SendMail;

/**
 *
 * @author Minh Luc
 */
@ManagedBean
@SessionScoped
public class FeedBackManagement implements Serializable {

    @EJB
    private FeedbackFacadeLocal feedbackFacade;
    private Feedback feedback = new Feedback();
    private Feedback selectFeedback = new Feedback();
    private List<Feedback> list = getFeedBackList();
    private List<Feedback> filteredList = list;
    private SelectItem[] statusOption = new SelectItem[]{
        new SelectItem("", "All"), new SelectItem("Pending", "Pending"), new SelectItem("Complete", "Complete")
    };

    public void create() {
        if (!validate(feedback)) {
            return;
        }


        RequestContext context = RequestContext.getCurrentInstance();
        FacesMessage msg = null;
        try {
            feedback.setFeedbackDate(new Date());
            feedback.setFeedStatus("Pending");
            feedback.setUserSend(System.user);
            feedbackFacade.create(feedback);
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Create ", "Create Successful");
        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", e.getMessage() + " Duplicate");
        }
        feedback = new Feedback();
        list = getFeedBackList();
        FacesContext.getCurrentInstance().addMessage(null, msg);
        context.addCallbackParam("create", true);
    }

    public void edit() {
        if (!validate2(selectFeedback)) {
            return;
        }

        RequestContext context = RequestContext.getCurrentInstance();
        FacesMessage msg = null;

        try {
            selectFeedback.setUserReply(System.user);
            selectFeedback.setFeedStatus("Complete");
            SendMail.send(selectFeedback.getUserSend().getEmail(), "Alert", "Manager :" + System.user.getUserName() + " answered your Feedback");
            feedbackFacade.edit(selectFeedback);
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Reply ", "Answer Successful");
        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", e.getMessage());
        }
        selectFeedback = new Feedback();
        list = getFeedBackList();
        FacesContext.getCurrentInstance().addMessage(null, msg);
        context.addCallbackParam("edit", true);
    }

    private List<Feedback> getFeedBackList() {
        if (System.user != null) {
            if (System.Manager) {
                return feedbackFacade.findAll();
            } else {
                return feedbackFacade.findByUserIdSend(System.user.getUserId());
            }
        } else {
            return null;
        }

    }

    public Feedback getFeedback() {
        return feedback;
    }

    public void setFeedback(Feedback feedback) {
        this.feedback = feedback;
    }

    public Feedback getSelectFeedback() {
        return selectFeedback;
    }

    public void setSelectFeedback(Feedback selectFeedback) {
        this.selectFeedback = selectFeedback;
    }

    public List<Feedback> getList() {
        list = getFeedBackList();
        return list;
    }

    public void setList(List<Feedback> list) {
        this.list = list;
    }

    public List<Feedback> getFilteredList() {
        return filteredList;
    }

    public void setFilteredList(List<Feedback> filteredList) {
        this.filteredList = filteredList;
    }

    public SelectItem[] getStatusOption() {
        return statusOption;
    }

    public void setStatusOption(SelectItem[] statusOption) {
        this.statusOption = statusOption;
    }

    public boolean validate(Feedback feedback) {
        RequestContext context = RequestContext.getCurrentInstance();
        FacesMessage msg = null;

        if (feedback.getFeedBackTitle() == null || feedback.getFeedBackContent().length() == 0) {
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "FeedbackTitle is not null");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return false;
        } else {
            if (feedback.getFeedBackTitle().length() < 2) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "FeedbackTitle is greater 2 Characters");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
            if (!util.Validate.isContent(feedback.getFeedBackTitle())) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "FeedbackTitle is only Character");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
        }

        if (feedback.getFeedBackContent() == null || feedback.getFeedBackContent().length() == 0) {
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "FeedbackContent is not null");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return false;
        } else {
            if (feedback.getFeedBackContent().length() < 2 || feedback.getFeedBackContent().length() > 300) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "FeedbackContent is from 2 - 300 Characters");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
            if (!util.Validate.isContent(feedback.getFeedBackContent())) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "FeedbackContent is only Character");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
        }

        return true;
    }

    public boolean validate2(Feedback feedback) {
        RequestContext context = RequestContext.getCurrentInstance();
        FacesMessage msg = null;

        if (feedback.getReplyContent() == null || feedback.getReplyContent().length() == 0) {
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "ReplyContent is not null");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return false;
        } else {
            if (feedback.getReplyContent().length() < 2 || feedback.getReplyContent().length() > 300) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "ReplyContent is from 2 - 300 Characters");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
            if (!util.Validate.isContent(feedback.getReplyContent())) {
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error", "ReplyContent is only Character");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return false;
            }
        }
        return true;
    }
}
